//http://datajq.xjistedu.cn/cas/login?service=http://njw.xjistedu.cn:8100/xjlgxyhd/loginSso?toMenu=xs_kcb
//function scheduleHtmlProvider() {
    async function scheduleHtmlProvider() {
        await loadTool('AIScheduleTools')
        await AIScheduleAlert('1.导入问题可至Q3231045040反馈\n2.点击确定后的卡顿等待片刻就好')
        //alert("1.遇导入问题可至Q3231045040反馈\n2.导入过程如卡顿请耐心等待")
    
        try {
            let forToken = 'http://njw.xjistedu.cn:8100/xjlgxyhd/loginSso?toMenu=xs_kcb'
            var token = get(forToken)
            token = token.substring(token.indexOf('token=') + 6, token.indexOf('&'))
            log('token', token)
        } catch (error) {
            await AIScheduleAlert(error.message + '\n请确定是否登录进入课表界面')
            return 'do not continue'
        }
        let forJcId = 'http://njw.xjistedu.cn:8100/xjlgxyhd/Get_sjkbms'
        let jcid = post(forJcId, token)
        jcid = JSON.parse(jcid)
        jcid = jcid.data[0]['kbjcmsid']
        log('kbjcmsid', jcid)
        let forWeek = 'http://njw.xjistedu.cn:8100/xjlgxyhd/teachingWeek'
        let week = post(forWeek, token)
        week = JSON.parse(week)['data']
        week = week.length
        log('总周数', week)
    
        let forCourse = 'http://njw.xjistedu.cn:8100/xjlgxyhd/student/curriculum?kbjcmsid=' + jcid + '&week='
        let fc, data
        var courseArr = new Array()
        for (c = 1; c <= week; c++) {
            fc = forCourse + c
            data = post(fc, token)
            data = JSON.parse(data)['data']
            courseArr.push(JSON.stringify(data))
        }
        return JSON.stringify(courseArr)
    }
    
    function get(url) {
        let xhr = new XMLHttpRequest()
        xhr.open('GET', url, false)
        xhr.send()
        return xhr.responseText
    }
    /* async function get(url) {
        // Default options are marked with *
        const response = await fetch(url, {
            method: 'GET', // *GET, POST, PUT, DELETE, etc.
            mode: 'cors', // no-cors, *cors, same-origin
            cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
            credentials: 'same-origin', // include, *same-origin, omit
            token: token,
            headers: {
                //'Content-Type': 'application/json'
                 'Content-Type': 'application/x-www-form-urlencoded',
            },
            redirect: 'follow', // manual, *follow, error
            referrerPolicy: 'no-referrer', // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url
        });
        return response.text; // parses JSON response into native JavaScript objects
    } */
    
    function post(url, token) {
        let xhr = new XMLHttpRequest()
        xhr.open('POST', url, false)
        //xhr.setRequestHeader("Access-Control-Allow-Origin", "*")
        xhr.setRequestHeader('token', token)
        xhr.send()
        return xhr.responseText
    }
    /* async function post(url, token) {
        // Default options are marked with *
        const response = await fetch(url, {
            method: 'POST', // *GET, POST, PUT, DELETE, etc.
            mode: 'cors', // no-cors, *cors, same-origin
            cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
            credentials: 'same-origin', // include, *same-origin, omit
            token: token,
            headers: {
                'Content-Type': 'application/json'
                // 'Content-Type': 'application/x-www-form-urlencoded',
            },
            redirect: 'follow', // manual, *follow, error
            referrerPolicy: 'no-referrer', // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url
        });
        return response.json(); // parses JSON response into native JavaScript objects
    } */
    
    function log(tag, ct) {
        if (ct == undefined)
            console.info(tag)
        else
            console.info(tag, ct)
    }